package com.caifu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caifu.mapper.SysUserMapper;
import com.caifu.pojo.SysMenu;
import com.caifu.mapper.SysMenuMapper;
import com.caifu.pojo.SysUser;
import com.caifu.service.SysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caifu.util.DesUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author Lyf
 * @since 2020-07-07
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Resource
    private SysMenuMapper menuMapper;
    @Resource
    private SysUserMapper userMapper;

    @Override
    public List<Map<String,Object>> getMenuByUser(int userNo) throws Exception{
        try{
            if(userNo == 1) {
                return menuMapper.getMenuAll();
            }else{
                SysUser user = userMapper.selectOne(new QueryWrapper<SysUser>().eq("user_no",userNo));
                if(user==null){
                    throw new Exception("SysUser查询失败，userNo="+userNo);
                }
                //未修改密码时，只允许访问个人中心修改密码
//                if("0".equals(user.getPasswordEditFlag())){
//                    return menuMapper.getPasswordMenu();
//                }
                //审计用户只能查看审计日志
//                if(userNo == 170){
//                    return menuMapper.getAuditMenu();
//                }else{
                    //return menuMapper.getUserMenu(6,userNo);
                    return menuMapper.getUserPermissionMenu(userNo);
                //}
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception("获取用户菜单失败！");
        }
    }

    @Override
    public List<Map<String, Object>> getUserMenu(int userNo, String menuNo) throws Exception{
        List<Map<String,Object>> lv1list = new ArrayList<>();
        try{
            lv1list = menuMapper.selectMaps(new QueryWrapper<SysMenu>().select("MENU_NO, NAME, ICON").eq("PARENT_NO",menuNo).orderByAsc("SORT"));
            lv1list.forEach(lv1->{
                int count=0;
                int lv1Menu = (int) lv1.get("MENU_NO");
                List<Map<String,Object>> lv2List = menuMapper.getUserMenu(lv1Menu,userNo);
                lv1.put("data",lv2List);
                lv1.put("count",lv2List.size());
            });
            return lv1list;
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception("获取菜单失败！");
        }
    }

    @Override
    public int getFirstShowMenu(int userNo) throws Exception {
        //未配置模块时，默认服务平台
        int firstMenu = 19;
        String appSet = menuMapper.getUserPlatSet(userNo);
        if(StringUtils.isNotBlank(appSet)){
            //有配置模块时，优先显示数据看板，其次按赋权顺序（排除平台管理）取第一个
            if(appSet.indexOf("89")!=-1){ //数据看板
                firstMenu = 89;
            }else{
                String[] arr = appSet.split(",");
                for (int i=0;i<arr.length;i++) {
                    if(arr[i].equals("1")){
                        continue;
                    }
                    firstMenu= Integer.parseInt(arr[i]);
                    break;
                }
            }
        }
        return firstMenu;
    }
}
